<?php
/**
 * @package Component Metromap for Joomla! 1.5
 * @version $Id: metromap.php 4 2010-08-17 13:58:48Z batyrkin $
 * @author Batyrkin Anton
 * @copyright (C) 2010- Batyrkin Anton
 * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 **/

defined('_JEXEC') or die();
jimport( 'joomla.application.component.model' );

class MetromapModelMetromap extends JModel
{
	var $_sityid		= null;
	var $_stationid		= null;
	var $_data			= null;
	var $_defsityid		= null;
	var $_defsityname	= null;

	function __construct() {
		parent::__construct();
		$defsityid  = $this->getDefSityID();
		$sityid 	= JRequest::getInt('sityid', $defsityid);
		$stationid 	= JRequest::getInt('stationid', 0);
		if ($sityid != $defsityid) {
			$stationid = 0;
		}
		$this->setId($sityid, $stationid);
	}

	function setId($sityid, $stationid) {
		$this->_sityid		= $sityid;
		$this->_stationid	= $stationid;
		$this->_data		= null;
	}

	function &getDefSityID() {
		global $mainframe;

		$db =& JFactory::getDBO();
		$params	=& $mainframe->getParams();
		$defsityname = $params->get( 'defsityname', 'Москва' );
		$this->_defsityname = $defsityname;
		$query = 'SELECT id'
			.' FROM #__categories'
			.' WHERE section="com_metromap"'
			.' AND title='.$db->Quote($defsityname);
		$db->setQuery( $query );
		if (!$db->query()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}else{
			$this->_defsityid = (int)$this->_db->loadResult(); //loadObject
			return $this->_defsityid;
		}
	}

	// список ВСЕХ опубликованых станций
	function &getStationList() {
		$query = 'SELECT id AS value, stationname AS text'
			.' FROM #__metrostations'
			.' WHERE published = 1'
			.' ORDER BY stationname';
		$this->_db->setQuery($query);
		$this->_StList = $this->_db->loadAssocList();
		return $this->_StList;
	}

	// список ВСЕХ опубликованых станций, у которых поле ссылка не пустое
	function &getStationListWithLink() {
		$query = 'SELECT stationlink AS value, stationname AS text'
			.' FROM #__metrostations'
			.' WHERE length(stationlink) > 0'
			.' AND published = 1'
			.' ORDER BY stationname';
		$this->_db->setQuery($query);
		$this->_StList = $this->_db->loadAssocList();
		return $this->_StList;
	}

	// список опубликованых станций к которым есть привязанные опубликованные точки
	function &getExistStationList() {
		$query = 'SELECT DISTINCT s.id AS value, s.stationname AS text'
			.' FROM #__metrostations AS s'
			.' LEFT JOIN #__metropoints AS p ON s.id = p.stationid'
			.' WHERE (p.published = 1 AND s.published = 1)'
			.' ORDER BY s.stationname';
		$this->_db->setQuery($query);
		$this->_StExistList = $this->_db->loadAssocList();
		return $this->_StExistList;
	}

	// список ВСЕХ опубликованых городов
	function &getSityList() {
		$query = 'SELECT id AS value, title AS text'
			.' FROM #__categories'
			.' WHERE section="com_metromap"'
			.' AND published=1'
			.' ORDER BY ordering, title';
		$this->_db->setQuery($query);
		$this->_SityList = $this->_db->loadAssocList();
		return $this->_SityList;
	}

	// список опубликованых городов к которым есть привязанные опубликованные точки
	function &getExistSityList() {
		$query = 'SELECT DISTINCT c.id AS value, c.title AS text'
			.' FROM #__categories AS c'
			.' LEFT JOIN #__metropoints AS p ON c.id = p.catid'
			.' WHERE c.section="com_metromap"'
			.' AND p.catid > 0'
			.' AND (c.published=1 AND p.published = 1)'
			.' ORDER BY c.ordering, c.title';
		$this->_db->setQuery($query);
		$this->_SityList = $this->_db->loadAssocList();
		return $this->_SityList;
	}

	function &getData() {

		if (!$this->_loadData()) {
			$this->_initData();
		}
		return $this->_data;
	}

	function &getDataLink() {

			if ($this->_sityid != $this->_defsityid) {
				$query = 'SELECT a.*,b.stationname'
						.' FROM #__metropoints AS a'
						.' LEFT JOIN #__metrostations AS b ON a.stationid=b.id'
						.' WHERE a.catid = '.(int) $this->_sityid;
				if ($this->_stationid > 0) {
					$query .= ' AND a.stationid = '.(int) $this->_stationid;
				}
				$this->_db->setQuery($query);
				$this->_data['points'] = $this->_db->loadObjectList();
			}

			$query = 'SELECT s.*'
				.' FROM #__metrostations AS s'
				.' WHERE length(stationlink) > 0'
				.' AND published = 1'
				.' ORDER BY stationname';
			$this->_db->setQuery($query);
			$this->_data['stations'] = $this->_db->loadObjectList();

			return $this->_data;
	}

	function _loadData() {
		if (empty($this->_data)) {

			$query = 'SELECT p.*,s.stationname'
					.' FROM #__metropoints AS p'
					.' LEFT JOIN #__metrostations AS s ON p.stationid=s.id'
					.' WHERE p.published=1'
					.' AND p.catid = '.(int) $this->_sityid;
			if ($this->_sityid == $this->_defsityid) {
				$query .= ' AND s.published=1';
			}
			if ($this->_stationid > 0) {
				$query .= ' AND p.stationid = '.(int) $this->_stationid;
			}
			$this->_db->setQuery($query);
			$this->_data['points'] = $this->_db->loadObjectList();

			$query = 'SELECT DISTINCT s.*'
					.' FROM #__metrostations AS s'
					.' LEFT JOIN #__metropoints AS p ON s.id = p.stationid'
					.' WHERE p.published = 1 AND s.published = 1';
			$this->_db->setQuery($query);
			$this->_data['stations'] = $this->_db->loadObjectList();

			return (boolean) $this->_data;
		}

		return true;
	}

	function _initData() {
		if (empty($this->_data)) {
			$this->_data	= '';
			return (boolean) $this->_data;
		}
		return true;
	}
}